//
//  HRZCommodityViewController.m
//  EMarket
//
//  Created by 石头 on 2018/5/14.
//  Copyright © 2018年 好日子. All rights reserved.
//

#import "HRZCommodityViewController.h"

// Controllers
#import "HRZCommodityGoodsViewController.h"
#import "HRZSearchMainViewController.h"
// Models
#import "HRZGoodsClassifyBaseModel.h"
// Views

// Vendors
#import <MJExtension.h>
#import "SPSlideSwitch.h"
// Categories
#import "UIView+SPExtension.h"

// Others
#import "HRZGlobalNetManager.h"

#define tableView_Width  95

@interface HRZCommodityViewController ()<SPSlideSwitchDelegate>
/* 搜索 */
@property (strong , nonatomic)UIView *topSearchView;
/* 搜索按钮 */
@property (strong , nonatomic)UIButton *searchButton;
/* 语音按钮 */
@property (strong , nonatomic)UIButton *voiceButton;
/* 左边数据 */
@property (strong , nonatomic)NSArray<HRZGoodsClassifyBaseModel *> *titleItem;
/** 分页器 */
@property (nonatomic, strong) SPSlideSwitch *slideSwitch;

@end

@implementation HRZCommodityViewController

#pragma mark - LazyLoad
- (NSArray<HRZGoodsClassifyBaseModel *> *)titleItem {
    if (!_titleItem) {
        _titleItem = [[NSArray alloc] init];
    }
    return _titleItem;
}


#pragma mark - LifeCyle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    
    
    [self setNaviBar];
    
    [self setupData];
    
    [self setUpUI];
}


#pragma mark - -- 加载数据 ---
/** 获取数据 */
- (void)setupData {
    
    WEAKSELF
    [[HRZGlobalNetManager getGlobalNetManager] onSearchGoodsClassifyComplete:^(NSArray<HRZGoodsClassifyBaseModel *> *result, BOOL isSuc) {
        
        if (isSuc) {
            
            weakSelf.titleItem = result;
            [weakSelf setUpUI];
        }else {
            NSMutableArray *arr = [HRZGoodsClassifyBaseModel mj_objectArrayWithFilename:@"ClassifyTitles.plist"];
            weakSelf.titleItem = arr;
            [weakSelf setUpUI];
//            weakSelf.titleItem = nil;
        }
    }];
    
}


/** 设置UI */
- (void)setUpUI {
    self.isHidenNaviBar = YES;
    
    NSMutableArray *titleArray = [[NSMutableArray alloc] init];
    NSMutableArray *viewCArray = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < _titleItem.count; i++) {
        HRZGoodsClassifyBaseModel *item = _titleItem[i];
        [titleArray addObject:item.className];
        
        HRZCommodityGoodsViewController *goodsVC = [[HRZCommodityGoodsViewController alloc] init];
        goodsVC.classifyModel = item;
        [viewCArray addObject:goodsVC];
        
    }
    
    if (viewCArray.count == 0) {
        return;
    }
    
    /** 生成分页器 */
    _slideSwitch = [[SPSlideSwitch alloc] initWithFrame:CGRectMake(0, HRZTopNavH, kScreenWidth, kScreenHeight - HRZTopNavH - HRZBottomTabH) slideDirection:SPSlideSegmentedDirectionVerticality Titles:titleArray viewControllers:viewCArray];
    _slideSwitch.delegate = self;
    _slideSwitch.itemSelectedColor = HRZThemeColor;
    _slideSwitch.itemNormalColor = [UIColor blackColor];
    _slideSwitch.customTitleSpacing = 2;
    [_slideSwitch showInViewController:self];
}

#pragma mark - 设置导航栏
- (void)setNaviBar {
    
    _topSearchView = [[UIView alloc] init];
    _topSearchView.backgroundColor = HRZBGColor;
    _topSearchView.layer.cornerRadius = 5;
    [_topSearchView.layer masksToBounds];
    _topSearchView.frame = CGRectMake(10, 26, kScreenWidth - 20, 32);
    [self.view addSubview:_topSearchView];
    
    _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_searchButton setTitle:@"🔍搜索商品" forState:0];
    [_searchButton setTitleColor:[UIColor lightGrayColor] forState:0];
    _searchButton.titleLabel.font = PFR12Font;
    [_searchButton setImage:[UIImage imageNamed:@""] forState:0];
    [_searchButton adjustsImageWhenHighlighted];
    _searchButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _searchButton.titleEdgeInsets = UIEdgeInsetsMake(0, 2* HRZMargin, 0, 0);
    _searchButton.imageEdgeInsets = UIEdgeInsetsMake(0, HRZMargin, 0, 0);
    [_searchButton addTarget:self action:@selector(onSearchButtonClick) forControlEvents:UIControlEventTouchUpInside];
    _searchButton.frame = CGRectMake(0, 0, _topSearchView.sp_width - 2 * HRZMargin, _topSearchView.sp_height);
    [_topSearchView addSubview:_searchButton];
}

#pragma mark - ---------<SPSlideSwitchDelegate>----------
- (void)slideSwitchDidselectAtIndex:(NSInteger)index {
    
}

#pragma mark - -------------Action-------------
- (void)onSearchButtonClick {
    HRZSearchMainViewController *searchVC = [[HRZSearchMainViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:NO];
}


@end
